<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>闭包陷阱</title>
</head>

<body>
  <button id="btn">add</button>
  <script>
    //存在Dom不存在情况，出现内存泄漏
    function foo() {
      var el = document.getElementById('btn');
      el.onclick = function () {
        console.log(el.id)
      }
    }

    //清空闭包内引用对象
    function foo() {
      var el = document.getElementById('btn');
      el.onclick = function () {
        console.log(el.id)
      }
      el = null;//引用对象清空
    }

    foo();
  </script>
</body>

</html>